﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKCoordinate;

namespace YJKRevitPipeSetting.Utiltiy
{
	public class ThreePipeInputDataPacker : InputDataPackerBase
	{
		internal void SetEnd(bool ifEnd = true)
		{
			this.ifEnd = ifEnd;
			this.makeBoundryCurvePacker();
		}

		internal ThreePipeInputDataPacker(TwoPipeInputDataPakcer inputOne, TwoPipeInputDataPakcer inputTwo, XYZ realStartPoint = null, bool ifEnd = false)
		{
			this.ifEnd = ifEnd;
			this.realStartPoint = realStartPoint;
			if (!inputOne.GetPoint(1).IsAlmostEqualTo(inputTwo.GetPoint(0)))
			{
				throw new CornerMakerException(StaticRescource.StrNonContinuePoint);
			}
			this.useFamilyFileIndexs.Clear();
			this.useFamilyFileIndexs.Add(inputOne.GetFamilyFileIndex(0));
			this.useFamilyFileIndexs.Add(inputTwo.GetFamilyFileIndex(0));
			this.lstPoints.Clear();
			this.lstPoints.Add(inputOne.GetPoint(0));
			this.lstPoints.Add(inputOne.GetPoint(1));
			this.lstPoints.Add(inputTwo.GetPoint(1));
			this.driection.Clear();
			this.driection.Add(inputOne.GetPoint(1).Add(-inputOne.GetPoint(0)).Normalize());
			this.driection.Add(inputTwo.GetPoint(0).Add(-inputTwo.GetPoint(1)).Normalize());
			if (this.driection[0].IsAlmostEqualTo(this.driection[1]) || this.driection[0].IsAlmostEqualTo(-this.driection[1]))
			{
				this.ifIsCornerMid = false;
			}
			else
			{
				this.ifIsCornerMid = false;
			}
			this.lstTwoInputDataPackers.Clear();
			this.lstTwoInputDataPackers.Add(inputOne);
			this.lstTwoInputDataPackers.Add(inputTwo);
			this.lstPipeInputParameter.Clear();
			this.lstPipeInputParameter.Add(inputOne.GetPipeParameters(1));
			this.lstPipeInputParameter.Add(inputTwo.GetPipeParameters(1));
			this.makeBoundryCurvePacker();
			this.finalCheck();
		}

		internal XYZ GetEndPoint()
		{
			return this.realEndPoint;
		}

		internal List<PipeBoundryCurvePakcer> GetLstBoundryCurvePacker()
		{
			return this.lstBoundryCurvePacker;
		}

		internal XYZ GetDriection(int index)
		{
			if (index == 0)
			{
				return this.driection[0];
			}
			return this.driection[1];
		}

		internal TwoPipeInputDataPakcer GetTwoInputData(int index)
		{
			if (index == 0)
			{
				return this.lstTwoInputDataPackers[0];
			}
			return this.lstTwoInputDataPackers[1];
		}

		private void makeBoundryCurvePacker()
		{
			this.lstBoundryCurvePacker.Clear();
			double[] valueParams = new double[]
			{
				this.lstTwoInputDataPackers[0].GetPipeParameters(1)[PipeUseParameterEnum.Width],
				this.lstTwoInputDataPackers[0].GetPipeParameters(1)[PipeUseParameterEnum.Height],
				this.lstTwoInputDataPackers[0].GetPipeParameters(1)[PipeUseParameterEnum.DistanceFloor],
				(double)this.lstTwoInputDataPackers[0].GetFamilyFileIndex(0)
			};
			double[] array = new double[]
			{
				this.lstTwoInputDataPackers[0].GetPipeParameters(1)[PipeUseParameterEnum.Width],
				this.lstTwoInputDataPackers[0].GetPipeParameters(1)[PipeUseParameterEnum.Height],
				this.lstTwoInputDataPackers[0].GetPipeParameters(1)[PipeUseParameterEnum.DistanceFloor],
				(double)this.lstTwoInputDataPackers[1].GetFamilyFileIndex(0)
			};
			List<XYZ> list = new List<XYZ>();
			if (!this.ifIsCornerMid)
			{
				if (!this.ifEnd)
				{
					if (this.realStartPoint != null)
					{
						List<Curve> normalLineBoundryCurves = Utiltiy.GetNormalLineBoundryCurves(this.realStartPoint, this.lstPoints[1], this.lstTwoInputDataPackers[0].GetPipeParameters(1), ref list);
						PipeBoundryCurvePakcer item = new PipeBoundryCurvePakcer(this.realStartPoint, this.lstPoints[1], normalLineBoundryCurves, false, valueParams);
						this.lstBoundryCurvePacker.Add(item);
					}
					else
					{
						int count = this.lstPoints.Count;
						List<Curve> normalLineBoundryCurves2 = Utiltiy.GetNormalLineBoundryCurves(this.lstPoints[count - 3], this.lstPoints[count - 2], this.lstTwoInputDataPackers[0].GetPipeParameters(1), ref list);
						PipeBoundryCurvePakcer item2 = new PipeBoundryCurvePakcer(this.lstPoints[count - 3], this.lstPoints[count - 2], normalLineBoundryCurves2, false, valueParams);
						this.lstBoundryCurvePacker.Add(item2);
					}
					this.realEndPoint = this.lstPoints[1];
				}
				else
				{
					int count2 = this.lstPoints.Count;
					PipeBoundryCurvePakcer item3;
					if (this.realStartPoint != null)
					{
						List<Curve> normalLineBoundryCurves3 = Utiltiy.GetNormalLineBoundryCurves(this.realStartPoint, this.lstPoints[1], this.lstTwoInputDataPackers[0].GetPipeParameters(1), ref list);
						item3 = new PipeBoundryCurvePakcer(this.realStartPoint, this.lstPoints[1], normalLineBoundryCurves3, false, valueParams);
						this.lstBoundryCurvePacker.Add(item3);
					}
					else
					{
						List<Curve> normalLineBoundryCurves3 = Utiltiy.GetNormalLineBoundryCurves(this.lstPoints[count2 - 3], this.lstPoints[count2 - 2], this.lstTwoInputDataPackers[0].GetPipeParameters(1), ref list);
						item3 = new PipeBoundryCurvePakcer(this.lstPoints[count2 - 3], this.lstPoints[count2 - 2], normalLineBoundryCurves3, false, valueParams);
						this.lstBoundryCurvePacker.Add(item3);
					}
					List<Curve> normalLineBoundryCurves4 = Utiltiy.GetNormalLineBoundryCurves(this.lstPoints[count2 - 2], this.lstPoints[count2 - 1], this.lstTwoInputDataPackers[1].GetPipeParameters(1), ref list);
					item3 = new PipeBoundryCurvePakcer(this.lstPoints[count2 - 2], this.lstPoints[count2 - 1], normalLineBoundryCurves4, false, valueParams);
					this.lstBoundryCurvePacker.Add(item3);
					this.realEndPoint = this.lstPoints[2];
				}
			}
			this.lstRectPoints = list;
		}

		private void finalCheck()
		{
			if (this.ifIsCornerMid)
			{
				if (this.realStartPoint == null)
				{
					if (this.lstBoundryCurvePacker[1].GetPoint(0).DistanceTo(this.lstPoints[1]) > this.lstPoints[0].DistanceTo(this.lstPoints[1]))
					{
						throw new CornerMakerException(StaticRescource.StrCornerOutSide);
					}
				}
				else if (this.lstBoundryCurvePacker[1].GetPoint(0).DistanceTo(this.lstPoints[1]) > this.realStartPoint.DistanceTo(this.lstPoints[1]))
				{
					throw new CornerMakerException(StaticRescource.StrCornerOutSide);
				}
				if (this.lstTwoInputDataPackers[1].GetPoint(1).DistanceTo(this.lstPoints[1]) > this.lstPoints[2].DistanceTo(this.lstPoints[1]))
				{
					throw new CornerMakerException(StaticRescource.StrCornerOutSide);
				}
			}
		}

		private bool ifIsCornerMid;

		private bool ifEnd;

		private XYZ realStartPoint;

		private XYZ realEndPoint;

		private List<XYZ> driection = new List<XYZ>();

		private List<TwoPipeInputDataPakcer> lstTwoInputDataPackers = new List<TwoPipeInputDataPakcer>();

		private PipeIMakeCornerCurves boundartCurveMaker;

		private List<PipeBoundryCurvePakcer> lstBoundryCurvePacker = new List<PipeBoundryCurvePakcer>();

		public List<XYZ> lstRectPoints = new List<XYZ>();
	}
}
